package com.julis.thread;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.Constant;
import com.android.scj.tc.TcAppFolw;
import com.baseutils.SpfUtils;
import com.util.DelayTimeThread;
import com.util.HexUtil;

import java.io.IOException;
import java.util.NoSuchElementException;

import com.julis.bean.FanHui;


/**
 * 脱机检测
 */
public class DetectionSubThread extends Thread {

	private static TcAppFolw equiCon  ;
	public static Bundle bundle; // 封装显示数据
	public static boolean isSucess=false;
	
	public DelayTimeThread delayThread = null;
	// 测试时间
	public static long time;
	public static int type = 0;//0:正常检测 1:激活前检测
	Handler handler;
	public DetectionSubThread(Context context, Handler handler){
		this.   handler=   handler;
		boolean	psamopen = (boolean) SpfUtils.get(context, Constant.Check_Psam, true);
	  equiCon =  new TcAppFolw(context,psamopen);
	}


	public int detection() throws NoSuchElementException,
            NumberFormatException, IOException, Exception {
		time = System.currentTimeMillis();

		equiCon.ObuOfflineExmin();
		
		long lTmpTime = System.currentTimeMillis();
		time =  lTmpTime - time;
		//检测OBU是否有卡
		if (type!=0) {
			String ICCard = equiCon.GetCardSerialNumber();
			if (TextUtils.isEmpty(ICCard)) {
				return 1;
			}
			int nclass = Integer.parseInt(equiCon.VClass);
			if (nclass<1 || (nclass>5 && nclass<=10) || nclass>15&& nclass<=20) {
				return 2;
			}
		}

		return 0;
	}
	private void LoadBundMess()throws Exception
	{
		bundle = new Bundle();
		bundle.putString("OBUMAC", ""+ equiCon.GetOBUMAC());
		bundle.putString("OBUSERIALNUMBER",""+ equiCon.GetOBUSerialNumber());
		bundle.putString("OBUSTATUS",""+  equiCon.GetObuStatus());
		bundle.putString("OBUISSUER", ""+ HexUtil.HexToAsciiString(equiCon.GetOBUArea()));
		bundle.putString("OBUCONTRACTTYPE",""+  equiCon.GetObuContractType());
		bundle.putString("OBUCONTRACT", ""+ equiCon.GetObuContract());
		bundle.putString("OBUBATTERY", ""+ equiCon.GetObuBattery());
		bundle.putString("OBUDATE", ""+ TcAppFolw.convertDate(equiCon.GetObuDate().substring(0, 8)));
		bundle.putString("OBUDATE2",""+  TcAppFolw.convertDate(equiCon.GetObuDate().substring(8, 16)));

		bundle.putString("VLP",""+ equiCon.VLP);
		bundle.putString("VLPC",""+ equiCon.VLPC);
		bundle.putString("VLPC_1",""+  equiCon.VLPC);
		bundle.putString("VCLASS",""+ equiCon.VClass);
		bundle.putString("VUSERTYPE",""+  equiCon.VUserType);
		bundle.putString("VSIZE", ""+ equiCon.VSize);
		bundle.putString("VSIZE2",""+  equiCon.VSize2);
		bundle.putString("VWHEEL", ""+ equiCon.VWheel);
		bundle.putString("VAXIS", ""+ equiCon.VAxis);
		bundle.putString("VAXISWIDTH",""+  equiCon.VAxisWidth);
		bundle.putString("VWeight", ""+ equiCon.VWeight);
		bundle.putString("VDESCIBE", ""+ equiCon.VDescribe);
		bundle.putString("VENGINESN",""+  equiCon.VEngineSN);
		
		bundle.putString("CARDISSUER",""+ equiCon.GetCardIssuer());
		bundle.putString("CARDTYPE", ""+ equiCon.GetCardType());
		bundle.putString("CARDVERSION", ""+ equiCon.GetCardVersion());
		bundle.putString("CARDNETNUMBER", ""+ equiCon.GetCardNetNumber());
		bundle.putString("CARDSERIALNUMBER", ""+ equiCon.GetCardSerialNumber());
		bundle.putString("CARDVLP",""+ equiCon.GetCardVlp());
		bundle.putString("CARDVLPC",""+ equiCon.GetCardVehicleColor());
		bundle.putString("CARDUSERTYPE",""+ equiCon.GetCardUserType());
		bundle.putString("CARDBALANCE", ""+ equiCon.GetCardBalance());
		bundle.putString("CARDDATE", ""+ equiCon.GetCardDate1());
		bundle.putString("CARDDATE2",""+ equiCon.GetCardDate2());
			
	}
	@Override
	public void run() {
		try {
			int nRet = detection();
			LoadBundMess();
			switch (nRet) {
			case 1:
				isSucess = false;
				throw new NumberFormatException("用户卡号不存在，请插卡");
			case 2:
				isSucess = false;
				throw new NumberFormatException("未知车型");
			default:
				break;
			}
			isSucess = true;
			Message message=new Message();
			FanHui fanHui=new FanHui();
			fanHui.setMessage("success脱机检测成功");
			fanHui.setBundle(bundle);
			message.what=1;
			message.obj=fanHui;
			handler.sendMessage(message);

			Log.e("test", "数据：脱机检测成功" );
		} catch (Exception e) {
			if (delayThread!=null) {
				delayThread.isDelay(false);
			}
			Message message=new Message();
			message.what=2;
			FanHui fanHui=new FanHui();
			fanHui.setMessage("Error脱机检测：" + e.getMessage());
			message.obj=fanHui;
			handler.sendMessage(message);

			Log.e("test", "数据：脱机检测失败"+ e.getMessage() );
		}
	}
	
	public void print(String key, String value){
		Log.e(key, value+"");
	}
}
